﻿using AutoMapper;
using Common.Property;
using DapperExtensions;
using DapperExtensions.Predicate;
using Domain.Com;
using DTO;
using DTO.DTO.Com.BasicType;
using IRepository.Com;
using IServices.Com;

namespace Services.Com
{
    public class BasicTypeService : IBasicTypeService
    {
        private IMapper _mapper;
        private IBasicTypeRepository _basicTypeRepository;
        public BasicTypeService(IMapper mapper, IBasicTypeRepository basicTypeRepository)
        {
            _mapper = mapper;
            _basicTypeRepository = basicTypeRepository;
        }

        public async Task<bool> Delete(string id, OperatorInfo op)
        {
            return await _basicTypeRepository.DeleteAsync(id, op);
        }

        public async Task<bool> DeleteBatch(List<string> ids, OperatorInfo op)
        {
            return await _basicTypeRepository.DeleteBatchAsync(ids, op);
        }

        public async Task<BasicType> GetById(string id)
        {
            return await _basicTypeRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<BasicType>> GetByIds(List<string> ids)
        {
            return await _basicTypeRepository.GetByIdsAsync(ids);
        }

        public async Task<Tuple<IEnumerable<BasicType>, int>> GetPageList(BasicTypeQueryDTO dto)
        {
            if(dto.ParentId == null)
            {
                dto.ParentId = "";
            }
            var pgb = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
            pgb.Predicates.Add(Predicates.Field<BasicType>(f => f.Type, Operator.Eq, dto.Type));
            pgb.Predicates.Add(Predicates.Field<BasicType>(f => f.ParentId, Operator.Eq, dto.ParentId));
            if (!string.IsNullOrWhiteSpace(dto.Keywords))
            {
                pgb.Predicates.Add(Predicates.Field<BasicType>(f => f.Title, Operator.Like, $"%{dto.Keywords}%"));
            }
            var sort = new List<ISort>();
            sort.Add(new Sort { PropertyName = "Sort", Ascending = true });

            var result = await _basicTypeRepository.GetPageListAsync(dto.PageIndex, dto.PageSize, pgb, sort);
            return result;
        }

        public async Task<IEnumerable<BasicType>> GetList(BasicTypeQueryDTO dto)
        {
            if (dto.ParentId == null)
            {
                dto.ParentId = "";
            }
            var pgb = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
            pgb.Predicates.Add(Predicates.Field<BasicType>(f => f.Type, Operator.Eq, dto.Type));
            pgb.Predicates.Add(Predicates.Field<BasicType>(f => f.ParentId, Operator.Eq, dto.ParentId));
            if (!string.IsNullOrWhiteSpace(dto.Keywords))
            {
                pgb.Predicates.Add(Predicates.Field<BasicType>(f => f.Title, Operator.Like, $"%{dto.Keywords}%"));
            }
            pgb.Predicates.Add(Predicates.Field<BasicType>(f => f.Status, Operator.Eq, E_Status.Enabled.ToString()));
            var sort = new List<ISort>();
            sort.Add(new Sort { PropertyName = "Sort", Ascending = true });

            var result = await _basicTypeRepository.GetListAsync(pgb, sort);
            return result;
        }

        public async Task<Result> Save(BasicTypeDetailDTO dto, OperatorInfo op)
        {
            if (dto.ParentId == null)
            {
                dto.ParentId = "";
            }
            if (string.IsNullOrWhiteSpace(dto.Id))
            {
                var model = _mapper.Map<BasicType>(dto);
                var status = await _basicTypeRepository.InsertAsync(model, op);

                return status ? Result.Success("保存成功", model.Id) : Result.Failed(-1, "保存失败");
            }
            else
            {
                var model = await _basicTypeRepository.GetByIdAsync(dto.Id);
                _mapper.Map(dto, model);
                var status = await _basicTypeRepository.UpdateAsync(model, op);
                return status ? Result.Success("保存成功", model.Id) : Result.Failed(-1, "保存失败");
            }
        }
    }
}
